//index.js
//获取应用实例
const app = getApp();
const commonService = require('../../service/common.js');
const homeService = require('../../service/home.js');
const videoService = require('../../service/video.js');

Page({
  data: {
    title:"发布视频",
    form:{
      title:"",
      content:"",
      site:"",
      line:"",
      picList:[],
    },
    len: 0,
    city:"",
    multiIndex:[0,0],
    multiArray:[],
    id:"",
    videoName:"",
    videoContent:""
  },
  onLoad: function (options) {
    this.setData({
      city: wx.getStorageSync("DT-CITY"),
      id:options.id || ""
    })
    this.init();
  },
  init(){
    this.getLineList();
  },
  updateList(e) {
    let fileList = e.detail.fileList;
    this.data.form.picList = fileList;
  },
  inputContent(e){
    let val = e.detail.value;
    this.setData({
      len: val.length
    })
  },
  formReset(e){},
  bindMultiPickerChange(e){
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      multiIndex: e.detail.value
    })
  },
  bindMultiPickerColumnChange(e){
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    if(e.detail.column == 1) return false;
    let key = `multiIndex[${e.detail.column}]`;
    this.setData({
      "multiIndex[0]":e.detail.value,
      "multiIndex[1]":0
    })
    this.getSiteList();
  },
  getLineList(){
    commonService.getLineList({}).then(res => {
      if (res.resCode == 0) {
        this.setData({
          "multiArray[0]": [...res.object]
        });
        console.log(this.data.multiArray);
        this.getSiteList();
      }
    }).catch(err => {
    })
  },
  //获取站点列表
  getSiteList(fn = ()=>{}) {
    let d = {
      lineId: this.data.multiArray[0][this.data.multiIndex[0]].lineId
    }
    console.log(d);
    commonService.getSiteList(d).then(res => {
      if (res.resCode == 0) {
        let data = res.object.map(v => v.lineName = v.siteName);
        this.setData({
          "multiArray[1]": res.object
        });
        fn(res.object);
      }
    })
  },
  formSubmit(e){
    console.log(e);
    let val = e.detail.value;
    let data = {
      ...val,
      type: 0,
      unionId: app.globalData.userinfo.unionId,
      site: this.data.multiArray[1][this.data.multiIndex[1]].lineName,
      line:this.data.multiArray[0][this.data.multiIndex[0]].lineName,
      unionId: app.globalData.userinfo.unionId,
      activityId: this.data.id,
      picList: this.data.form.picList.map(v => v.name || "").join(","),
      city: wx.getStorageSync("DT-CITY"),
      videoName: this.data.form.picList.map(v => v.name || "").join(","),
      videoContent: this.data.content
    }
    console.log(data);
    
    if (data.content == "" ){
            wx.showModal({
                    title: '提醒',
                    content: '请输入视频述！',
                    showCancel: false,
                    confirmText: "确定",
                    success: (res) => {
                    }
            })
    } else if (data.picList == ""){
            wx.showModal({
                    title: '提醒',
                    content: '请上传视频！',
                    showCancel: false,
                    confirmText: "确定",
                    success: (res) => {
                    }
            })
    }else{
            videoService.addVideo(data).then(res => {
                    if (res.resCode == 0) {
                            wx.showToast({
                                    title: '发布成功',
                                    icon: "none"
                            })
                            wx.redirectTo({
                                    url: '/pages/my-video/index',
                            });
                    }
            })
    }
    
  }
})
